package com.dtp.core.support;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import com.dtp.common.entity.NotifyItem;
import com.dtp.core.thread.DtpExecutor;
import com.dtp.core.thread.ExecutorAdapter;

import lombok.Data;

/**
 * 执行器包装类
 *
 * @author yanhom
 * @since 1.0.3
 *
 * @remark muse
 **/
@Data
public class ExecutorWrapper {
    private String threadPoolName;          // 线程池名称
    private String threadPoolAliasName;     // 线程池别名
    private ExecutorAdapter<?> executor;    // Executor适配器
    private List<NotifyItem> notifyItems;   // 通知项集合（NotifyItemEnum）
    private List<String> platformIds;       // 通知平台id集合
    private boolean notifyEnabled = true;   // 是否开启通知

    /** 针对DtpExecutor创建包装器*/
    public ExecutorWrapper(DtpExecutor executor) {
        this.threadPoolName = executor.getThreadPoolName();
        this.executor = executor;
        this.notifyItems = executor.getNotifyItems();
        this.notifyEnabled = executor.isNotifyEnabled();
    }

    /** 针对JUC的Executor创建包装器*/
    public ExecutorWrapper(String threadPoolName, Executor executor) {
        this.threadPoolName = threadPoolName;
        if (executor instanceof ThreadPoolExecutor)
            this.executor = new ThreadPoolExecutorAdapter((ThreadPoolExecutor) executor);
        else if (executor instanceof ExecutorAdapter<?>)
            this.executor = (ExecutorAdapter<?>) executor;
        else
            throw new IllegalArgumentException("unsupported Executor type !");

        this.notifyItems = NotifyItem.getSimpleNotifyItems();
    }

    public static ExecutorWrapper of(DtpExecutor executor) {return new ExecutorWrapper(executor);}
}
